<template>
<div>
  <div>
    <H3 style="margin-left: 15px">请大家务必填写正确的个人信息!(姓名学号年级等信息错误请联系教务处)</H3>
    <el-form :model="info" ref="info" :rules="rules" hide-required-asterisk="true" label-position="left" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input
            v-model="info.name"
            style="width: 300px;padding-left: 30px"
            :disabled="true">
        </el-input>
      </el-form-item>

      <el-form-item label="学号" prop="id">
        <el-input
            v-model="info.id"
            style="width: 300px;padding-left: 30px"
            :disabled="true">
        </el-input>
      </el-form-item>

      <el-form-item label="年级" prop="grade">
        <el-input
            v-model="info.grade"
            style="width: 300px;padding-left: 30px"
            :disabled="true">
        </el-input>
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input
            v-model="info.email"
            style="width: 300px;padding-left: 30px">
        </el-input>
      </el-form-item>

      <el-form-item label="电话" prop="mobile">
        <el-input
            v-model="info.mobile"
            style="width: 300px;padding-left: 30px">
        </el-input>
      </el-form-item>

      <el-form-item style="margin-top: 80px;margin-left: 400px">
        <el-button type="primary" @click="submitForm('info')">保存</el-button>
      </el-form-item>
    </el-form>

  </div>
</div>
</template>

<script>
export default {
  name: "PersonalInfo",
  data() {
    return {
      info: {
        name: '',
        id: '',
        grade: '',
        email: '',
        mobile: ''
      },
      rules: {
        email: [
          {required: true, message: '请填写正确的邮箱', trigger: 'blur'}
        ],
        mobile: [
          {required: true, message: '请填写正确的电话', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    let userinfo = JSON.parse(window.sessionStorage.getItem('userinfo'))
    let user = JSON.parse(window.sessionStorage.getItem('user'))
    let _that = this
    setTimeout(() => {
      if (userinfo) {
        _that.info.name = userinfo.name
        _that.info.id = userinfo.userId
        _that.info.grade = userinfo.grade
      }
      if(user) {
        _that.info.email = user.email
        _that.info.mobile = user.mobile
      }
    },500)

  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.put(`${this.HOST}/user/changeInfo`,{
            info: this.info
          },{
            header: {
              'Content-Type': 'multipart/form-data'
            }
          }).then(res => {
            this.$message({
              message: res.data.message,
              type: 'success',
              offset: 90,
              duration: 1000
            })
          })
        } else {
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>
.el-form {
  position: absolute;
  left: 30px;
  top: 100px;
  width: 600px;
  height: 500px;
}
</style>
